﻿Imports LinhKienMayTinh.CommonMethods

Public Class SearchPage
    Inherits BasePage

#Region "Khao báo các biến toàn cục"


#End Region

#Region "Khai báo các event"

    ''' <summary>
    ''' SearchPage_Init
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub SearchPage_Init(sender As Object, e As System.EventArgs) Handles Me.Init
        Try
            Dim master As LinhKienMayTinhMasterPage = DirectCast(Me.Page.Master, LinhKienMayTinhMasterPage)
            'master.CssLinkAdd(ResolveUrl("~/Style/ProductList.css"))
            'master.CssLinkAdd(ResolveUrl("~/Style/PagingBarStyle.css"))
            master.CssLinkAdd(ResolveUrl("~/Style/ProductListMetroStyle.css"))
            master.CssLinkAdd(ResolveUrl("~/Style/PagingBarMetroStyle.css"))

            master.ScriptLinkAdd(ResolveUrl("~/Scripts/ProductList.js"))
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

    ''' <summary>
    ''' Page_Load
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim xProductName As String = String.Empty
        Dim nPage As Integer = 1
        Dim nProductPerPage As Integer = 1

        Try
            If IsNothing(Me.QueryString("keyword")) = False Then
                xProductName = Me.QueryString("keyword")
            Else
                Throw New Exception("NO_CATEGORY_FOUND")
            End If

            If IsNothing(Me.QueryString("page")) = False Then
                nPage = CInt(Me.QueryString("page"))
            End If

            If IsNothing(AppSettings("ProductPerPage")) = False Then
                nProductPerPage = CInt(AppSettings("ProductPerPage"))
            End If

            xProductName = xProductName.Trim()
            Call GetProductList(xProductName, nPage, nProductPerPage)
            ucPagingBar.Url = ResolveUrl("~/Search/keyword=" & xProductName)
            ucPagingBar.CurrentPage = nPage
            ucPagingBar.TotalPage = Math.Ceiling(GetProductCount(xProductName) / nProductPerPage)
            ucPagingBar.DataBind()

            Me.Header.Title = "Kết quả tìm kiếm với từ khóa '" & xProductName & "'"
            Me.lbTitle.Text = "Kết quả tìm kiếm với từ khóa '" & xProductName & "'"
        Catch ex As Exception
            Server.Transfer("~/Pages/Error.aspx")
        End Try
    End Sub

#End Region

#Region "Khai báo các function method"

    ''' <summary>
    ''' get danh sách sản phẩm
    ''' </summary>
    ''' <param name="pProductName"></param>
    ''' <param name="pPage"></param>
    ''' <param name="pProductPerPage"></param>
    ''' <remarks></remarks>
    Private Sub GetProductList(pProductName As String, pPage As Integer, pProductPerPage As Integer)

        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess
        Dim nStart As Integer
        Dim nEnd As Integer

        Try
            dataAccess = Me.GetDBAccess()

            nStart = (pPage * pProductPerPage) - (pProductPerPage - 1)
            nEnd = pPage * pProductPerPage

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        DAT.*"
            xSql = xSql & vbCrLf & "FROM"
            xSql = xSql & vbCrLf & "("
            xSql = xSql & vbCrLf & "SELECT"
            xSql = xSql & vbCrLf & "        ROW_NUMBER() OVER(ORDER BY T_PRODUCT.CODE,T_PRODUCT.NAME) AS ROW_NUM,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CODE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME          AS PRODUCT_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.LINK_NAME     AS PRODUCT_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.IMG_LINK,"
            xSql = xSql & vbCrLf & "		T_CATEGORY.LINK_NAME    AS CATEGORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		T_MANUFACTORY.LINK_NAME AS MANUFACTORY_LINK_NAME,"
            xSql = xSql & vbCrLf & "		ISNULL(T_PRODUCT.PRICE, 0) AS PRICE,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.SPECIFICATION,"
            xSql = xSql & vbCrLf & "		T_PRODUCT.DESCRIPTION"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND ("
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & "OR		T_CATEGORY.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & "OR		T_MANUFACTORY.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & ")) DAT"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        DAT.ROW_NUM BETWEEN @START AND @END"

            xSql = Replace(xSql, "@NAME", ToSqlStr(pProductName, SqlDataType.DataType_String, False))
            xSql = Replace(xSql, "@START", ToSqlStr(nStart, SqlDataType.DataType_Integer))
            xSql = Replace(xSql, "@END", ToSqlStr(nEnd, SqlDataType.DataType_Integer))

            ds = dataAccess.GetDBDataset(xSql)

            Me.ucProductList.DataSource = ds
            Me.ucProductList.DataBind()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    ''' <summary>
    ''' get số lượng item thỏa điều kiện
    ''' </summary>
    ''' <param name="pProductName"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetProductCount(pProductName As String) As Integer
        Dim xSql As String
        Dim ds As DataSet
        Dim dataAccess As DBAccess

        Try
            dataAccess = Me.GetDBAccess()

            xSql = ""
            xSql = xSql & vbCrLf & "SELECT COUNT(*) as ROW_COUNT"
            xSql = xSql & vbCrLf & "FROM (T_PRODUCT LEFT JOIN T_CATEGORY ON"
            xSql = xSql & vbCrLf & "		T_PRODUCT.CATEGORY = T_CATEGORY.CODE)"
            xSql = xSql & vbCrLf & "LEFT JOIN T_MANUFACTORY"
            xSql = xSql & vbCrLf & "		ON T_PRODUCT.MANUFACTORY = T_MANUFACTORY.CODE"
            xSql = xSql & vbCrLf & "WHERE"
            xSql = xSql & vbCrLf & "        T_PRODUCT.DEL_FLG = 0"
            xSql = xSql & vbCrLf & "AND ("
            xSql = xSql & vbCrLf & "		T_PRODUCT.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & "OR		T_CATEGORY.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & "OR		T_MANUFACTORY.NAME LIKE N'%@NAME%'"
            xSql = xSql & vbCrLf & ")"

            xSql = Replace(xSql, "@NAME", ToSqlStr(pProductName, SqlDataType.DataType_String, False))

            ds = dataAccess.GetDBDataset(xSql)

            If IsNothing(ds) OrElse ds.Tables(0).Rows.Count < 1 Then
                Throw New Exception("NO_DATA_FOUND")
            End If

            Return CInt(ds.Tables(0).Rows(0)("ROW_COUNT"))
        Catch ex As Exception
            Return 0
        End Try
    End Function

#End Region
End Class